import { useRoute, useRouter } from "vue-router";

export function vHeaderEvent() {
  // 当前路由
  const currentRoute = useRoute();
  const router = useRouter();
  // 刷新页面
  const reloadPage = async () => {
    await nextTick();
    setTimeout(() => {
      document.documentElement.scrollTo({ left: 0, top: 0 });
    }, 100);
    console.log(currentRoute.fullPath);
    console.log(router);
    // vue的router路由跳转刷新页面，页面的数据不会重新加载，经查阅是vue生命周期的问题，使用使用 window.location.reload();刷新当前页面
    // window.location.reload();
    // router.push({
    //   path: "/redirect" + unref(currentRoute).fullPath,
    // });
  };

  // TODO 这里还有any类型还没改, 应该改成菜单类型.
  const generator: any = (routerMap: any) => {
    return routerMap.map((item: any) => {
      const currentMenu = {
        ...item,
        label: item.meta.title,
        key: item.name,
        disabled: item.path === "/",
      };
      // 是否有子菜单，并递归处理
      if (item.children && item.children.length > 0) {
        // Recursion
        currentMenu.children = generator(item.children, currentMenu);
      }
      return currentMenu;
    });
  };

  /**
   * 页头面包屑导航的路由跳转
   * @param key
   */
  const dropdownSelect = (key: string) => {
    router.push({ name: key });
  };
  return {
    reloadPage,
    generator,
    dropdownSelect,
  };
}
